import {
  getProjectPurchaseList,
  getCusDocs,
  addProjectPurchase,
  updateProjectPurchase,
  getTenderList,
  getFollowInfo,
  updateFllowInfo,
  addTender,
  deleteTender
} from "@/api/projectPurchase";

function getDetail() {
  let detail = {};
  try {
    detail = JSON.parse(sessionStorage.getItem("projectPurchaseDetail"));
  } catch (error) {
    return undefined;
  }
  return detail;
}

const state = {
  projectPurchaseDetail: getDetail() || {}
};
const actions = {
  getProjectPurchaseList({}, payload) {
    return new Promise((resolve, reject) => {
      getProjectPurchaseList(payload)
        .then(res => {
          resolve(res);
        })
        .catch(err => {
          reject(err);
        });
    });
  },
  getCusDocs({}, payload) {
    return new Promise((resolve, reject) => {
      getCusDocs(payload)
        .then(res => {
          resolve(res);
        })
        .catch(err => {
          reject(err);
        });
    });
  },
  addProjectPurchase({}, payload) {
    return new Promise((resolve, reject) => {
      addProjectPurchase(payload)
        .then(res => {
          resolve(res);
        })
        .catch(err => {
          reject(err);
        });
    });
  },
  updateProjectPurchase({}, payload) {
    return new Promise((resolve, reject) => {
      updateProjectPurchase(payload)
        .then(res => {
          resolve(res);
        })
        .catch(err => {
          reject(err);
        });
    });
  },
  getTenderList({}, payload) {
    return new Promise((resolve, reject) => {
      getTenderList(payload)
        .then(res => {
          resolve(res);
        })
        .catch(err => {
          reject(err);
        });
    });
  },
  getFollowInfo({}, payload) {
    return new Promise((resolve, reject) => {
      getFollowInfo(payload)
        .then(res => {
          resolve(res);
        })
        .catch(err => {
          reject(err);
        });
    });
  },
  updateFllowInfo({}, payload) {
    return new Promise((resolve, reject) => {
      updateFllowInfo(payload)
        .then(res => {
          resolve(res);
        })
        .catch(err => {
          reject(err);
        });
    });
  },
  addTender({}, payload) {
    return new Promise((resolve, reject) => {
      addTender(payload)
        .then(res => {
          resolve(res);
        })
        .catch(err => {
          reject(err);
        });
    });
  },
  deleteTender({}, payload) {
    return new Promise((resolve, reject) => {
      deleteTender(payload)
        .then(res => {
          resolve(res);
        })
        .catch(err => {
          reject(err);
        });
    });
  }
};
const mutations = {
  setDocsDetail(state, payload) {
    sessionStorage.setItem("projectPurchaseDetail", JSON.stringify(payload));
    state.projectPurchaseDetail = payload;
  },
  resetDocsDetail(state, payload) {
    sessionStorage.removeItem("projectPurchaseDetail");
    state.projectPurchaseDetail = {};
  }
};
const getters = {};
export default {
  namespaced: true,
  state,
  actions,
  mutations,
  getters
};
